/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QtDebug>
#include "dlgcamcon.h"
#include <QRect>
#include <iostream>
#include <QSettings>
#include "qhkp.h"

#define DEBUG_ON    0
#define DEBUG if(DEBUG_ON)

DlgCamCon::DlgCamCon(HPSCEventThread *hpscEventThread,QWidget *parent)
 : QWidget(parent),
 m_commSettings(NULL),
 m_power(NULL)
// m_hkpSocket(NULL),
// m_hkpTimer(NULL)
{
  int headerHight = 101;
  QRect rect;
  this->setAutoFillBackground(true);
  ui.setupUi(this);
  QPixmap bgpixmap = QPixmap(":images/modules/camcon/bkg_camcon.png");
  if(bgpixmap.isNull()) std::cout<<"DlgCamCon: null background pixmap"<<std::endl;
  QPalette p = this->palette();
  p.setBrush(this->backgroundRole(),QBrush(bgpixmap));
  this->setPalette(p);
   
  m_power = new FrmPower(FrmPower::A,FrmPower::B,this);
  m_power->setHPSCEventThread(hpscEventThread);
  QString serPort;
  QSettings settings("../../gss.conf",QSettings::IniFormat);
  serPort = settings.value("Serial_Ports/CamCon").toString();
  
  //m_hkpHost = settings.value("hosts/HKP_IP"/*,"127.0.0.1"*/).toString();
  //m_hkpPort = settings.value("hosts/HKP_Port"/*,3010*/).toInt();
  
  std::cout<<"CamCon serial port:"<<serPort.toAscii().data()<<std::endl;
  m_commSettings = new DlgCommSettings(serPort,this);
  
  //rect.setTop(101);
  rect.setRect(0,headerHight,580,250);
  m_power->setGeometry(rect);
  m_commSettings->setGeometry(m_power->width()+40,headerHight+70,409,206);
  
  ui.stkSections->insertWidget(0,new SecCamConMain());
  ui.stkSections->insertWidget(1,new SecCamConCams());
  ui.stkSections->insertWidget(2,new SecCamConServos());
  ui.stkSections->setCurrentIndex(0);
  
  //m_hkpSocket = new QTcpSocket();
  //m_hkpTimer = new QTimer();
  
  connect(m_commSettings,SIGNAL(sig_serialSettings()),this,SLOT(serialSettings()));
  //connect(m_hkpSocket,SIGNAL(readyRead()),this,SLOT(readHkp()));
  //connect(m_hkpTimer,SIGNAL(timeout()),this,SLOT(submitRequestsToHkp()));
  
  //m_hkpSocket->connectToHost(m_hkpHost,m_hkpPort);
  //m_hkpTimer->start(250);
  connect(this,SIGNAL(sig_hkp(HkpCommand)),this,SLOT(readHkp(HkpCommand)));
  connect(this,SIGNAL(sig_hkp(HkpCommand)),m_power,SLOT(getHkpStatus(HkpCommand)));
  connect(m_power,SIGNAL(sig_busStatus(FrmPower::BUS,FrmPower::POWER_STAT)),this,SIGNAL(sig_busStatus(FrmPower::BUS,FrmPower::POWER_STAT)));

}


DlgCamCon::~DlgCamCon()
{
}

/*!
    \fn DlgCamCon::main()
 */
void DlgCamCon::selectSection(int i)
{
	ui.stkSections->setCurrentIndex(i);
}

/*!
    \fn DlgCamCon::serialSettings()
 */
void DlgCamCon::serialSettings()
{
  emit sig_serialSettings(CAMCON);
}


/*!
    \fn DlgCamCon::reloadSerialSettings()
 */
void DlgCamCon::reloadSerialSettings()
{
    m_commSettings->loadConfig(CAMCON);
}


/*!
    \fn DlgCamCon::commSettigsDlg()
 */
DlgCommSettings * DlgCamCon::commSettigsDlg()
{
    return m_commSettings;
}


/*!
    \fn DlgCamCon::readHkp()
 */
void DlgCamCon::readHkp(HkpCommand hkpPacket)
{
    //qDebug()<<"DlgCamCon::readHkp "<<hkpPacket.data;;

    if(hkpPacket.port == QHkp::A)
    {
        if(hkpPacket.type == READ_IN)
        {
            QLabel *umbilical = ((SecCamConMain*)ui.stkSections->widget(0))->ui.lblUmbilical;
            if(hkpPacket.data & UMBILICAL_CONN) 
            {
                if(umbilical->text()!="Connected")
                {
                    QPalette p = umbilical->palette();  
                    p.setColor(QPalette::WindowText,Qt::green);
                    umbilical->setPalette(p); 
                    umbilical->setText("Connected");
                }
            }
            else 
                {
                    if(umbilical->text()!="Not Connected") 
                    {
                        QPalette p = umbilical->palette();  
                        p.setColor(QPalette::WindowText,Qt::red);
                        umbilical->setPalette(p); 
                        umbilical->setText("Not Connected");
                    }
                }
        }
    }
}

